package edu.bit.water.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.bit.water.entity.LabelEvaCtestSum;
import edu.bit.water.service.LabelEvaCtestSumService;

@Controller
@RequestMapping("/label_eva_ctest_sum")
public class LabelEvaCtestSumController {
	
	@Autowired
	public LabelEvaCtestSumService labelEvaCtestSumService;
	
	/**
	 * 查询数据
	 * @return 真实数据或是错误信息
	 */
	@ResponseBody
	@RequestMapping(value = "/select", method = RequestMethod.GET)
	public Map<String, Object> queryLabelEvaCtestSum() {
		Map<String, Object> modelMap = new HashMap<>();
		List<LabelEvaCtestSum> labelEvaCtestSumList = new ArrayList<LabelEvaCtestSum>();
		try {
			labelEvaCtestSumList = labelEvaCtestSumService.queryLabelEvaCtestSum();
			modelMap.put("status", "success");
			modelMap.put("rows", labelEvaCtestSumList);
		} catch (Exception e) {
			modelMap.put("status", "failed");
			modelMap.put("msg", e.getMessage());
		}
		return modelMap;
	}
	
	/**
	 * 查询数据
	 * @return 真实数据或是错误信息
	 */
	@ResponseBody
	@RequestMapping(value = "/select2", method = RequestMethod.GET)
	public Map<String, Object> queryLabelEvaCtestSum2(int id) {
		Map<String, Object> modelMap = new HashMap<>();
		List<Double> labelEvaCtestSumList = new ArrayList<>();
		try {
			labelEvaCtestSumList = labelEvaCtestSumService.queryLabelEvaCtestSum2(id);
			modelMap.put("status", "success");
			modelMap.put("rows", labelEvaCtestSumList);
		} catch (Exception e) {
			modelMap.put("status", "failed");
			modelMap.put("msg", e.getMessage());
		}
		return modelMap;
	}
	
	/**
	 * 插入数据
	 * @param labelEvaCtestSum
	 * @return 成功或失败信息放入map中
	 */
	@ResponseBody
	@RequestMapping("/insert")
	public Map<String, Object> insertLabelEvaCtestSum(LabelEvaCtestSum labelEvaCtestSum) {
		return labelEvaCtestSumService.insertLabelEvaCtestSum(labelEvaCtestSum);
	}
	/**
	 * 
	 * @param listId 删除数据组id号
	 * @return 成功或失败信息放入map中
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public Map<String, Object> deleteLabelEvaCtestSum(@RequestBody List<Integer> listId){
		int flag = labelEvaCtestSumService.deleteLabelEvaCtestSum(listId);
		Map<String, Object> modelMap = new HashMap<>();
		if(flag == 1) {
			modelMap.put("status", "success");
		}else {
			modelMap.put("status", "failed");
		}
		return modelMap;
	}
	
	/**
	 * 跟新标签表
     * @param list 所更新的数据
     * @return 操作后的信息
     */
    @RequestMapping("/update")
    @ResponseBody
    public Map<String, Object> updateLabelEvaCtestSum(@RequestBody List<Object> list) {
        Map<String, Object> modelMap = new HashMap<>();
        LabelEvaCtestSum labelEvaCtestSum = new LabelEvaCtestSum();
        labelEvaCtestSum.setId((int) list.remove(0));// id
        labelEvaCtestSum.setCreatedAt(list.remove(0).toString());//createdAt
        labelEvaCtestSum.setCompanyName(list.remove(0).toString());// companyName
        labelEvaCtestSum.setCompanyClass(list.remove(0).toString());// companyClass
        labelEvaCtestSum.setTestUnit(list.remove(0).toString());// testUnit
        labelEvaCtestSum.setTestPerson(list.remove(0).toString());//testPerson
        
        try {
        	if (labelEvaCtestSumService.updateLabelEvaCtestSum(labelEvaCtestSum) == 1) {
                modelMap.put("status", "success");
            } else {
                modelMap.put("status", "failed");
            }
        } catch (BadSqlGrammarException e) {
			modelMap.put("status", "failed");
			//modelMap.put("msg", "数据格式出错"+"\n具体报错:"+e.getMessage());
			modelMap.put("msg", "数据格式出错");
		} catch (DuplicateKeyException e) {
			modelMap.put("status", "failed");
			modelMap.put("msg", "违反唯一性约束");
		} catch (Exception e) {
			modelMap.put("status", "failed");
			modelMap.put("msg", "error class:" + e.getClass());
		}
        
        return modelMap;
    }
}
